package Lambda;

import org.junit.Before;
import org.junit.Test;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeMap;

public class LambdaDemo {
    private List<Employee> employeeList;

    @Before
    public void init(){
        employeeList=new ArrayList<>();
        employeeList.add(new Employee(1,"张三", 15,8000,"销售部"));
        employeeList.add(new Employee(2,"李四", 35,6000,"客服"));
        employeeList.add(new Employee(3,"王五", 25,10000,"财务部"));
        employeeList.add(new Employee(4,"赵六", 35,11000,"财务部"));
        employeeList.add(new Employee(5,"田七", 22,12000,"管理部"));
        employeeList.add(new Employee(6,"老八", 26,15000,"技术部"));
        employeeList.add(new Employee(7,"三炮", 27,9000,"技术部"));
        employeeList.add(new Employee(8,"李九", 29,11000,"管理部"));
        employeeList.add(new Employee(9,"周十", 48,18000,"技术部"));
    }


    public List<Employee> findEmployeesByAge(List<Employee> employeeList, MyPredicate<Employee> predicate){
        List<Employee> emps=new ArrayList<>();
        for (Employee employee:employeeList) {
            if(predicate.test(employee))
                emps.add(employee);
        }
        return emps;
    }

    @Test
    public void test1(){
        List<Employee> list = findEmployeesByAge(employeeList, new MyPredicate<Employee>() {
            @Override
            public boolean test(Employee employee) {
                return employee.getAge() > 20;
            }
        });
        System.out.println(list);
    }


    @Test
    public void test2(){
        List<Employee> employeesByAge = findEmployeesByAge(employeeList, (employee) -> employee.getAge() > 20);
        employeesByAge.forEach(System.out::println);
        //employeesByAge.forEach((employee)->{ System.out.println(employee); });
        //employeesByAge.forEach((employee)-> System.out.println(employee) );
    }

    @Test
    public void test3(){
        List<Employee> employeesByAge1 = findEmployeesByAge(employeeList, (employee) -> employee.getAge() > 20);
        List<Employee> employeesByAge2 = findEmployeesByAge(employeeList, (employee -> employee.getAge() > 40));
        List<Employee> employeesByAge3 = findEmployeesByAge(employeeList, (employee -> employee.getAge() > 50));
        List<Employee> employeesByAge4 = findEmployeesByAge(employeeList, (employee -> employee.getAge() > 10));
        List<Employee> employeesByAge5 = findEmployeesByAge(employeeList, (employee -> employee.getAge() > 15));
        List<Employee> employeesByAge6 = findEmployeesByAge(employeeList, (employee -> employee.getAge() > 60));
        List<Employee> employeesByAge7 = findEmployeesByAge(employeeList, (employee -> employee.getAge() > 70));
        employeesByAge1.forEach(System.out::println);
        employeesByAge2.forEach(System.out::println);
        employeesByAge3.forEach(System.out::println);
        employeesByAge4.forEach(System.out::println);
        employeesByAge5.forEach(System.out::println);
        employeesByAge6.forEach(System.out::println);
        employeesByAge7.forEach(System.out::println);
    }

    @Test
    public void test4(){
        employeeList.stream().filter((employee -> employee.getAge()>20)).forEach(System.out::println);
        employeeList.stream().filter((employee -> employee.getAge()>40)).forEach(System.out::println);
        employeeList.stream().filter((employee -> employee.getAge()>50)).forEach(System.out::println);
        employeeList.stream().filter((employee -> employee.getAge()>10)).forEach(System.out::println);
        employeeList.stream().filter((employee -> employee.getAge()>15)).forEach(System.out::println);
        employeeList.stream().filter((employee -> employee.getAge()>60)).forEach(System.out::println);
        employeeList.stream().filter((employee -> employee.getAge()>70)).forEach(System.out::println);
    }

    @Test
    public void test5(){
        //找出工资最高的那个
        employeeList.stream()
                .max((o1, o2) -> (int) (o1.getSalary()-o2.getSalary()))
                .ifPresent(System.out::println);
        //找出年龄最小的那个
        employeeList.stream()
                .min(Comparator.comparingInt(Employee::getAge))
                .ifPresent(System.out::println);
    }

    public static void testInnerClass(){
        //自定义一个比较器
        Comparator<String> comparator=(s1,s2)->{
            Collator collator = Collator.getInstance();
            return collator.getCollationKey(s1).compareTo(collator.getCollationKey(s2));
        };
        //这就是lambda表达式的使用，原来的方式是new Comparator接口，实现compare方法。
        // 使用lambda使得代码更简洁一些。

        TreeMap<String,String> treeMap=new TreeMap<>(comparator);
    }
}
